{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Filtering Rows " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import pandas \n", "import pandas as pd " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# read movie data \n", "movies = pd.read_csv(\"http://bit.ly/imdbratings\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
09.3The Shawshank RedemptionRCrime142[u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt...
19.2The GodfatherRCrime175[u'Marlon Brando', u'Al Pacino', u'James Caan']
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
39.0The Dark KnightPG-13Action152[u'Christian Bale', u'Heath Ledger', u'Aaron E...
48.9Pulp FictionRCrime154[u'John Travolta', u'Uma Thurman', u'Samuel L....
\n", "
" ], "text/plain": [ " star_rating title content_rating genre duration \\\n", "0 9.3 The Shawshank Redemption R Crime 142 \n", "1 9.2 The Godfather R Crime 175 \n", "2 9.1 The Godfather: Part II R Crime 200 \n", "3 9.0 The Dark Knight PG-13 Action 152 \n", "4 8.9 Pulp Fiction R Crime 154 \n", "\n", " actors_list \n", "0 [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt... \n", "1 [u'Marlon Brando', u'Al Pacino', u'James Caan'] \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "3 [u'Christian Bale', u'Heath Ledger', u'Aaron E... \n", "4 [u'John Travolta', u'Uma Thurman', u'Samuel L.... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# examine first few rows \n", "movies.head() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering Movies with `for` Loop" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "booleans = [] \n", "for length in movies.duration: \n", " if length >= 200: \n", " booleans.append(True)\n", " else: \n", " booleans.append(False)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "979" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check length of booleans \n", "len(booleans)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[False, False, True, False, False]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Inspect booleans elements \n", "booleans[0:5]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# create a pandas series \n", "is_long = pd.Series(booleans)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 True\n", "3 False\n", "4 False\n", "dtype: bool" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Inspect few values \n", "is_long.head() " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
78.9The Lord of the Rings: The Return of the KingPG-13Adventure201[u'Elijah Wood', u'Viggo Mortensen', u'Ian McK...
178.7Seven SamuraiUNRATEDDrama207[u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K...
788.4Once Upon a Time in AmericaRCrime229[u'Robert De Niro', u'James Woods', u'Elizabet...
858.4Lawrence of ArabiaPGAdventure216[u\"Peter O'Toole\", u'Alec Guinness', u'Anthony...
1428.3Lagaan: Once Upon a Time in IndiaPGAdventure224[u'Aamir Khan', u'Gracy Singh', u'Rachel Shell...
1578.2Gone with the WindGDrama238[u'Clark Gable', u'Vivien Leigh', u'Thomas Mit...
2048.1Ben-HurGAdventure212[u'Charlton Heston', u'Jack Hawkins', u'Stephe...
4457.9The Ten CommandmentsAPPROVEDAdventure220[u'Charlton Heston', u'Yul Brynner', u'Anne Ba...
4767.8HamletPG-13Drama242[u'Kenneth Branagh', u'Julie Christie', u'Dere...
6307.7Malcolm XPG-13Biography202[u'Denzel Washington', u'Angela Bassett', u'De...
7677.6It's a Mad, Mad, Mad, Mad WorldAPPROVEDAction205[u'Spencer Tracy', u'Milton Berle', u'Ethel Me...
\n", "
" ], "text/plain": [ " star_rating title \\\n", "2 9.1 The Godfather: Part II \n", "7 8.9 The Lord of the Rings: The Return of the King \n", "17 8.7 Seven Samurai \n", "78 8.4 Once Upon a Time in America \n", "85 8.4 Lawrence of Arabia \n", "142 8.3 Lagaan: Once Upon a Time in India \n", "157 8.2 Gone with the Wind \n", "204 8.1 Ben-Hur \n", "445 7.9 The Ten Commandments \n", "476 7.8 Hamlet \n", "630 7.7 Malcolm X \n", "767 7.6 It's a Mad, Mad, Mad, Mad World \n", "\n", " content_rating genre duration \\\n", "2 R Crime 200 \n", "7 PG-13 Adventure 201 \n", "17 UNRATED Drama 207 \n", "78 R Crime 229 \n", "85 PG Adventure 216 \n", "142 PG Adventure 224 \n", "157 G Drama 238 \n", "204 G Adventure 212 \n", "445 APPROVED Adventure 220 \n", "476 PG-13 Drama 242 \n", "630 PG-13 Biography 202 \n", "767 APPROVED Action 205 \n", "\n", " actors_list \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "7 [u'Elijah Wood', u'Viggo Mortensen', u'Ian McK... \n", "17 [u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K... \n", "78 [u'Robert De Niro', u'James Woods', u'Elizabet... \n", "85 [u\"Peter O'Toole\", u'Alec Guinness', u'Anthony... \n", "142 [u'Aamir Khan', u'Gracy Singh', u'Rachel Shell... \n", "157 [u'Clark Gable', u'Vivien Leigh', u'Thomas Mit... \n", "204 [u'Charlton Heston', u'Jack Hawkins', u'Stephe... \n", "445 [u'Charlton Heston', u'Yul Brynner', u'Anne Ba... \n", "476 [u'Kenneth Branagh', u'Julie Christie', u'Dere... \n", "630 [u'Denzel Washington', u'Angela Bassett', u'De... \n", "767 [u'Spencer Tracy', u'Milton Berle', u'Ethel Me... " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show dataframe all columns in duration 200 minutes \n", "movies[is_long]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering by Condition" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 True\n", "3 False\n", "4 False\n", "Name: duration, dtype: bool" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# filtering by conditions \n", "is_long = movies.duration >= 200\n", "is_long.head() " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
78.9The Lord of the Rings: The Return of the KingPG-13Adventure201[u'Elijah Wood', u'Viggo Mortensen', u'Ian McK...
178.7Seven SamuraiUNRATEDDrama207[u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K...
788.4Once Upon a Time in AmericaRCrime229[u'Robert De Niro', u'James Woods', u'Elizabet...
858.4Lawrence of ArabiaPGAdventure216[u\"Peter O'Toole\", u'Alec Guinness', u'Anthony...
1428.3Lagaan: Once Upon a Time in IndiaPGAdventure224[u'Aamir Khan', u'Gracy Singh', u'Rachel Shell...
1578.2Gone with the WindGDrama238[u'Clark Gable', u'Vivien Leigh', u'Thomas Mit...
2048.1Ben-HurGAdventure212[u'Charlton Heston', u'Jack Hawkins', u'Stephe...
4457.9The Ten CommandmentsAPPROVEDAdventure220[u'Charlton Heston', u'Yul Brynner', u'Anne Ba...
4767.8HamletPG-13Drama242[u'Kenneth Branagh', u'Julie Christie', u'Dere...
6307.7Malcolm XPG-13Biography202[u'Denzel Washington', u'Angela Bassett', u'De...
7677.6It's a Mad, Mad, Mad, Mad WorldAPPROVEDAction205[u'Spencer Tracy', u'Milton Berle', u'Ethel Me...
\n", "
" ], "text/plain": [ " star_rating title \\\n", "2 9.1 The Godfather: Part II \n", "7 8.9 The Lord of the Rings: The Return of the King \n", "17 8.7 Seven Samurai \n", "78 8.4 Once Upon a Time in America \n", "85 8.4 Lawrence of Arabia \n", "142 8.3 Lagaan: Once Upon a Time in India \n", "157 8.2 Gone with the Wind \n", "204 8.1 Ben-Hur \n", "445 7.9 The Ten Commandments \n", "476 7.8 Hamlet \n", "630 7.7 Malcolm X \n", "767 7.6 It's a Mad, Mad, Mad, Mad World \n", "\n", " content_rating genre duration \\\n", "2 R Crime 200 \n", "7 PG-13 Adventure 201 \n", "17 UNRATED Drama 207 \n", "78 R Crime 229 \n", "85 PG Adventure 216 \n", "142 PG Adventure 224 \n", "157 G Drama 238 \n", "204 G Adventure 212 \n", "445 APPROVED Adventure 220 \n", "476 PG-13 Drama 242 \n", "630 PG-13 Biography 202 \n", "767 APPROVED Action 205 \n", "\n", " actors_list \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "7 [u'Elijah Wood', u'Viggo Mortensen', u'Ian McK... \n", "17 [u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K... \n", "78 [u'Robert De Niro', u'James Woods', u'Elizabet... \n", "85 [u\"Peter O'Toole\", u'Alec Guinness', u'Anthony... \n", "142 [u'Aamir Khan', u'Gracy Singh', u'Rachel Shell... \n", "157 [u'Clark Gable', u'Vivien Leigh', u'Thomas Mit... \n", "204 [u'Charlton Heston', u'Jack Hawkins', u'Stephe... \n", "445 [u'Charlton Heston', u'Yul Brynner', u'Anne Ba... \n", "476 [u'Kenneth Branagh', u'Julie Christie', u'Dere... \n", "630 [u'Denzel Washington', u'Angela Bassett', u'De... \n", "767 [u'Spencer Tracy', u'Milton Berle', u'Ethel Me... " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show the rows duration >200\n", "movies[is_long]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering in DataFrame " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
78.9The Lord of the Rings: The Return of the KingPG-13Adventure201[u'Elijah Wood', u'Viggo Mortensen', u'Ian McK...
178.7Seven SamuraiUNRATEDDrama207[u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K...
788.4Once Upon a Time in AmericaRCrime229[u'Robert De Niro', u'James Woods', u'Elizabet...
858.4Lawrence of ArabiaPGAdventure216[u\"Peter O'Toole\", u'Alec Guinness', u'Anthony...
1428.3Lagaan: Once Upon a Time in IndiaPGAdventure224[u'Aamir Khan', u'Gracy Singh', u'Rachel Shell...
1578.2Gone with the WindGDrama238[u'Clark Gable', u'Vivien Leigh', u'Thomas Mit...
2048.1Ben-HurGAdventure212[u'Charlton Heston', u'Jack Hawkins', u'Stephe...
4457.9The Ten CommandmentsAPPROVEDAdventure220[u'Charlton Heston', u'Yul Brynner', u'Anne Ba...
4767.8HamletPG-13Drama242[u'Kenneth Branagh', u'Julie Christie', u'Dere...
6307.7Malcolm XPG-13Biography202[u'Denzel Washington', u'Angela Bassett', u'De...
7677.6It's a Mad, Mad, Mad, Mad WorldAPPROVEDAction205[u'Spencer Tracy', u'Milton Berle', u'Ethel Me...
\n", "
" ], "text/plain": [ " star_rating title \\\n", "2 9.1 The Godfather: Part II \n", "7 8.9 The Lord of the Rings: The Return of the King \n", "17 8.7 Seven Samurai \n", "78 8.4 Once Upon a Time in America \n", "85 8.4 Lawrence of Arabia \n", "142 8.3 Lagaan: Once Upon a Time in India \n", "157 8.2 Gone with the Wind \n", "204 8.1 Ben-Hur \n", "445 7.9 The Ten Commandments \n", "476 7.8 Hamlet \n", "630 7.7 Malcolm X \n", "767 7.6 It's a Mad, Mad, Mad, Mad World \n", "\n", " content_rating genre duration \\\n", "2 R Crime 200 \n", "7 PG-13 Adventure 201 \n", "17 UNRATED Drama 207 \n", "78 R Crime 229 \n", "85 PG Adventure 216 \n", "142 PG Adventure 224 \n", "157 G Drama 238 \n", "204 G Adventure 212 \n", "445 APPROVED Adventure 220 \n", "476 PG-13 Drama 242 \n", "630 PG-13 Biography 202 \n", "767 APPROVED Action 205 \n", "\n", " actors_list \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "7 [u'Elijah Wood', u'Viggo Mortensen', u'Ian McK... \n", "17 [u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K... \n", "78 [u'Robert De Niro', u'James Woods', u'Elizabet... \n", "85 [u\"Peter O'Toole\", u'Alec Guinness', u'Anthony... \n", "142 [u'Aamir Khan', u'Gracy Singh', u'Rachel Shell... \n", "157 [u'Clark Gable', u'Vivien Leigh', u'Thomas Mit... \n", "204 [u'Charlton Heston', u'Jack Hawkins', u'Stephe... \n", "445 [u'Charlton Heston', u'Yul Brynner', u'Anne Ba... \n", "476 [u'Kenneth Branagh', u'Julie Christie', u'Dere... \n", "630 [u'Denzel Washington', u'Angela Bassett', u'De... \n", "767 [u'Spencer Tracy', u'Milton Berle', u'Ethel Me... " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# filtering by columns \n", "movies[movies.duration >= 200]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2 Crime\n", "7 Adventure\n", "17 Drama\n", "78 Crime\n", "85 Adventure\n", "142 Adventure\n", "157 Drama\n", "204 Adventure\n", "445 Adventure\n", "476 Drama\n", "630 Biography\n", "767 Action\n", "Name: genre, dtype: object" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select only genre\n", "movies[movies.duration >= 200].genre" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2 Crime\n", "7 Adventure\n", "17 Drama\n", "78 Crime\n", "85 Adventure\n", "142 Adventure\n", "157 Drama\n", "204 Adventure\n", "445 Adventure\n", "476 Drama\n", "630 Biography\n", "767 Action\n", "Name: genre, dtype: object" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# same as above \n", "movies[movies.duration >= 200]['genre']" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2 Crime\n", "7 Adventure\n", "17 Drama\n", "78 Crime\n", "85 Adventure\n", "142 Adventure\n", "157 Drama\n", "204 Adventure\n", "445 Adventure\n", "476 Drama\n", "630 Biography\n", "767 Action\n", "Name: genre, dtype: object" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select columns by label \n", "movies.loc[movies.duration >= 200, 'genre']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple Filtering Criteria" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* True and True == True \n", "* True and False == False \n", "* True or True == True \n", "* True or False == True \n", "* False or False == False \n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
178.7Seven SamuraiUNRATEDDrama207[u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K...
1578.2Gone with the WindGDrama238[u'Clark Gable', u'Vivien Leigh', u'Thomas Mit...
4767.8HamletPG-13Drama242[u'Kenneth Branagh', u'Julie Christie', u'Dere...
\n", "
" ], "text/plain": [ " star_rating title content_rating genre duration \\\n", "17 8.7 Seven Samurai UNRATED Drama 207 \n", "157 8.2 Gone with the Wind G Drama 238 \n", "476 7.8 Hamlet PG-13 Drama 242 \n", "\n", " actors_list \n", "17 [u'Toshir\\xf4 Mifune', u'Takashi Shimura', u'K... \n", "157 [u'Clark Gable', u'Vivien Leigh', u'Thomas Mit... \n", "476 [u'Kenneth Branagh', u'Julie Christie', u'Dere... " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiple criteria \n", "movies[(movies.duration >= 200) & (movies.genre == 'Drama')]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
58.912 Angry MenNOT RATEDDrama96[u'Henry Fonda', u'Lee J. Cobb', u'Martin Bals...
78.9The Lord of the Rings: The Return of the KingPG-13Adventure201[u'Elijah Wood', u'Viggo Mortensen', u'Ian McK...
98.9Fight ClubRDrama139[u'Brad Pitt', u'Edward Norton', u'Helena Bonh...
138.8Forrest GumpPG-13Drama142[u'Tom Hanks', u'Robin Wright', u'Gary Sinise']
.....................
9587.4My Sister's KeeperPG-13Drama109[u'Cameron Diaz', u'Abigail Breslin', u'Alec B...
9687.4The English PatientRDrama162[u'Ralph Fiennes', u'Juliette Binoche', u'Will...
9707.4Wonder BoysRDrama107[u'Michael Douglas', u'Tobey Maguire', u'Franc...
9727.4Blue ValentineNC-17Drama112[u'Ryan Gosling', u'Michelle Williams', u'John...
9737.4The Cider House RulesPG-13Drama126[u'Tobey Maguire', u'Charlize Theron', u'Micha...
\n", "

287 rows × 6 columns

\n", "
" ], "text/plain": [ " star_rating title \\\n", "2 9.1 The Godfather: Part II \n", "5 8.9 12 Angry Men \n", "7 8.9 The Lord of the Rings: The Return of the King \n", "9 8.9 Fight Club \n", "13 8.8 Forrest Gump \n", ".. ... ... \n", "958 7.4 My Sister's Keeper \n", "968 7.4 The English Patient \n", "970 7.4 Wonder Boys \n", "972 7.4 Blue Valentine \n", "973 7.4 The Cider House Rules \n", "\n", " content_rating genre duration \\\n", "2 R Crime 200 \n", "5 NOT RATED Drama 96 \n", "7 PG-13 Adventure 201 \n", "9 R Drama 139 \n", "13 PG-13 Drama 142 \n", ".. ... ... ... \n", "958 PG-13 Drama 109 \n", "968 R Drama 162 \n", "970 R Drama 107 \n", "972 NC-17 Drama 112 \n", "973 PG-13 Drama 126 \n", "\n", " actors_list \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "5 [u'Henry Fonda', u'Lee J. Cobb', u'Martin Bals... \n", "7 [u'Elijah Wood', u'Viggo Mortensen', u'Ian McK... \n", "9 [u'Brad Pitt', u'Edward Norton', u'Helena Bonh... \n", "13 [u'Tom Hanks', u'Robin Wright', u'Gary Sinise'] \n", ".. ... \n", "958 [u'Cameron Diaz', u'Abigail Breslin', u'Alec B... \n", "968 [u'Ralph Fiennes', u'Juliette Binoche', u'Will... \n", "970 [u'Michael Douglas', u'Tobey Maguire', u'Franc... \n", "972 [u'Ryan Gosling', u'Michelle Williams', u'John... \n", "973 [u'Tobey Maguire', u'Charlize Theron', u'Micha... \n", "\n", "[287 rows x 6 columns]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiple criteria \n", "movies[(movies.duration >= 200) | (movies.genre == 'Drama')]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
09.3The Shawshank RedemptionRCrime142[u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt...
19.2The GodfatherRCrime175[u'Marlon Brando', u'Al Pacino', u'James Caan']
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
39.0The Dark KnightPG-13Action152[u'Christian Bale', u'Heath Ledger', u'Aaron E...
48.9Pulp FictionRCrime154[u'John Travolta', u'Uma Thurman', u'Samuel L....
.....................
9707.4Wonder BoysRDrama107[u'Michael Douglas', u'Tobey Maguire', u'Franc...
9727.4Blue ValentineNC-17Drama112[u'Ryan Gosling', u'Michelle Williams', u'John...
9737.4The Cider House RulesPG-13Drama126[u'Tobey Maguire', u'Charlize Theron', u'Micha...
9767.4Master and Commander: The Far Side of the WorldPG-13Action138[u'Russell Crowe', u'Paul Bettany', u'Billy Bo...
9787.4Wall StreetRCrime126[u'Charlie Sheen', u'Michael Douglas', u'Tamar...
\n", "

538 rows × 6 columns

\n", "
" ], "text/plain": [ " star_rating title \\\n", "0 9.3 The Shawshank Redemption \n", "1 9.2 The Godfather \n", "2 9.1 The Godfather: Part II \n", "3 9.0 The Dark Knight \n", "4 8.9 Pulp Fiction \n", ".. ... ... \n", "970 7.4 Wonder Boys \n", "972 7.4 Blue Valentine \n", "973 7.4 The Cider House Rules \n", "976 7.4 Master and Commander: The Far Side of the World \n", "978 7.4 Wall Street \n", "\n", " content_rating genre duration \\\n", "0 R Crime 142 \n", "1 R Crime 175 \n", "2 R Crime 200 \n", "3 PG-13 Action 152 \n", "4 R Crime 154 \n", ".. ... ... ... \n", "970 R Drama 107 \n", "972 NC-17 Drama 112 \n", "973 PG-13 Drama 126 \n", "976 PG-13 Action 138 \n", "978 R Crime 126 \n", "\n", " actors_list \n", "0 [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt... \n", "1 [u'Marlon Brando', u'Al Pacino', u'James Caan'] \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "3 [u'Christian Bale', u'Heath Ledger', u'Aaron E... \n", "4 [u'John Travolta', u'Uma Thurman', u'Samuel L.... \n", ".. ... \n", "970 [u'Michael Douglas', u'Tobey Maguire', u'Franc... \n", "972 [u'Ryan Gosling', u'Michelle Williams', u'John... \n", "973 [u'Tobey Maguire', u'Charlize Theron', u'Micha... \n", "976 [u'Russell Crowe', u'Paul Bettany', u'Billy Bo... \n", "978 [u'Charlie Sheen', u'Michael Douglas', u'Tamar... \n", "\n", "[538 rows x 6 columns]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiple or conditions \n", "movies[(movies.genre == \"Crime\") | (movies.genre == 'Drama') | (movies.genre == \"Action\")]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "3 True\n", "4 True\n", " ... \n", "974 False\n", "975 False\n", "976 True\n", "977 False\n", "978 True\n", "Name: genre, Length: 979, dtype: bool" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiple or using isin() method \n", "movies.genre.isin([\"Drama\", \"Action\", \"Crime\"])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
star_ratingtitlecontent_ratinggenredurationactors_list
09.3The Shawshank RedemptionRCrime142[u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt...
19.2The GodfatherRCrime175[u'Marlon Brando', u'Al Pacino', u'James Caan']
29.1The Godfather: Part IIRCrime200[u'Al Pacino', u'Robert De Niro', u'Robert Duv...
39.0The Dark KnightPG-13Action152[u'Christian Bale', u'Heath Ledger', u'Aaron E...
48.9Pulp FictionRCrime154[u'John Travolta', u'Uma Thurman', u'Samuel L....
.....................
9707.4Wonder BoysRDrama107[u'Michael Douglas', u'Tobey Maguire', u'Franc...
9727.4Blue ValentineNC-17Drama112[u'Ryan Gosling', u'Michelle Williams', u'John...
9737.4The Cider House RulesPG-13Drama126[u'Tobey Maguire', u'Charlize Theron', u'Micha...
9767.4Master and Commander: The Far Side of the WorldPG-13Action138[u'Russell Crowe', u'Paul Bettany', u'Billy Bo...
9787.4Wall StreetRCrime126[u'Charlie Sheen', u'Michael Douglas', u'Tamar...
\n", "

538 rows × 6 columns

\n", "
" ], "text/plain": [ " star_rating title \\\n", "0 9.3 The Shawshank Redemption \n", "1 9.2 The Godfather \n", "2 9.1 The Godfather: Part II \n", "3 9.0 The Dark Knight \n", "4 8.9 Pulp Fiction \n", ".. ... ... \n", "970 7.4 Wonder Boys \n", "972 7.4 Blue Valentine \n", "973 7.4 The Cider House Rules \n", "976 7.4 Master and Commander: The Far Side of the World \n", "978 7.4 Wall Street \n", "\n", " content_rating genre duration \\\n", "0 R Crime 142 \n", "1 R Crime 175 \n", "2 R Crime 200 \n", "3 PG-13 Action 152 \n", "4 R Crime 154 \n", ".. ... ... ... \n", "970 R Drama 107 \n", "972 NC-17 Drama 112 \n", "973 PG-13 Drama 126 \n", "976 PG-13 Action 138 \n", "978 R Crime 126 \n", "\n", " actors_list \n", "0 [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunt... \n", "1 [u'Marlon Brando', u'Al Pacino', u'James Caan'] \n", "2 [u'Al Pacino', u'Robert De Niro', u'Robert Duv... \n", "3 [u'Christian Bale', u'Heath Ledger', u'Aaron E... \n", "4 [u'John Travolta', u'Uma Thurman', u'Samuel L.... \n", ".. ... \n", "970 [u'Michael Douglas', u'Tobey Maguire', u'Franc... \n", "972 [u'Ryan Gosling', u'Michelle Williams', u'John... \n", "973 [u'Tobey Maguire', u'Charlize Theron', u'Micha... \n", "976 [u'Russell Crowe', u'Paul Bettany', u'Billy Bo... \n", "978 [u'Charlie Sheen', u'Michael Douglas', u'Tamar... \n", "\n", "[538 rows x 6 columns]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pass the series in DataFrame \n", "movies[movies.genre.isin([\"Drama\", \"Action\", \"Crime\"])]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }